<template>
  <div v-if="is_info" class="text-ellipsis" :title="date_str">{{date_str}}</div>
  <div v-else class="form_item-datetime_between flex" style="line-height: 0;">
    <input type="hidden" :name="name" :value="value">
    <radio v-if="add_radio" :config="radio_config" v-model:value="radio_value" button="1" :size="size" @change="onchange_radio"></radio>
    <div>
      <el-date-picker v-model="this_value" :format="format.template" :size="size" :type="format.type+'range'" @change="onchange"></el-date-picker>
    </div>
  </div>
</template>

<script>
import s94Mjs from "s94-js";
import radio from "@/components/auto_import/UnifyParam/radio.vue";

export default {
  _title_: '时间戳(区间)',
  _can_config_: true,
  _can_form_param_: true,
  _can_where_param_: true,
  name: "datetime_between",
  components: {radio},
  props: ['name', 'value', 'config', 'is_info', 'add_radio', 'size', 'is_list'],
  emits: ['update:value', 'change'],
  data(){
    let format = this.decode_format();
    return {
      format: format,
      this_value: this.value_arr(),
      need_display: true,
      radio_value: "",
      radio_config: this.date_range_map(),
    };
  },
  methods: {
    value_arr(){
      let v = s94Mjs.to_string(this.value);
      let arr = v.split(',');
      arr[1] = (arr[1] || 0)*1000;
      arr[0] *= 1000;
      return arr;
    },
    onchange(arr){
      let str_arr = [].concat(arr);
      if (str_arr){
        str_arr[0] = Math.floor(str_arr[0].getTime()/1000);
        str_arr[1] = Math.floor(str_arr[1].getTime()/1000);
      }
      let v = str_arr ? str_arr.join(',') : '';
      this.need_display = false;
      setTimeout(()=>{this.need_display = true;},5)
      this.$emit('update:value', v);
      this.$emit('change', v);
    },
    onchange_radio(v){
      this.$emit('update:value', v);
      this.$emit('change', v);
    },
    decode_format(){
      let config = this.config || 'Y-M-D H:I:S';
      let res = {}
      res.type = 'year';
      if (/[his]/i.test(config)){
        res.type = 'datetime';
      }else if(/d/i.test(config)){
        res.type = 'date';
      }else if(/m/i.test(config)){
        res.type = 'month';
      }
      let map = {Y:'YYYY',y:'YYYY',M:'MM',m:'M',D:'DD',d:'D',H:'HH',h:'H',I:'mm',i:'m',S:'ss',s:'s'}
      res.template = config.replace(/[YMDHISymdhis]/g, function (ms){
        return map[ms];
      })
      return res;
    },
    date_range_map(){
      let now_date = new Date();
      now_date.setHours(0, 0, 0, 0);
      let now_time = now_date.getTime()
      let day_week = now_date.getDay(); // 获取今天是星期几（0-6，其中0为星期日）
      day_week = day_week==0 ? 6 : day_week-1; //0表示星期一
      let year = now_date.getFullYear();
      let month = now_date.getMonth();
      let time_stamp = {
        'today': now_time,
        'today-1': now_time-1000*3600*24,
        'today+1': now_time+1000*3600*24,
        'week0': now_time-1000*3600*24*day_week,
        'week7': now_time+1000*3600*24*(7-day_week),
      }
      now_date.setDate(1);
      time_stamp['month'] = now_date.getTime();
      if (month==11){
        now_date.setMonth(0);
        time_stamp['year'] = now_date.getTime();
        now_date.setFullYear(year+1);
        time_stamp['month+1'] = now_date.getTime();
        time_stamp['year+1'] = now_date.getTime();
      }else {
        now_date.setMonth(month+1);
        time_stamp['month+1'] = now_date.getTime();

        now_date.setMonth(0);
        time_stamp['year'] = now_date.getTime();
        now_date.setFullYear(year+1);
        time_stamp['year+1'] = now_date.getTime();
      }
      let res = [
        {value:[time_stamp['today'],time_stamp['today+1']], content:'今天'},
        {value:[time_stamp['today-1'],time_stamp['today']], content:'昨天'},
        {value:[time_stamp['week0'],time_stamp['week7']], content:'本周'},
        {value:[time_stamp['month'],time_stamp['month+1']], content:'本月'},
        {value:[time_stamp['year'],time_stamp['year+1']], content:'本年'},
      ];
      let fmt = this.config || 'Y-M-D H:I:S';
      res.forEach((row)=>{
        let arr = row.value.map((v)=>{
          return Math.floor(v/1000);
        });
        row.value = arr.join(',');
      })
      return res;
    }
  },
  watch: {
    config(v, oldV){
      this.format = this.decode_format();
    },
    value(){
      if (!this.need_display) return;
      this.this_value = this.value_arr();
    }
  },
  computed: {
    date_str(){
      if (!this.value) return '';
      let arr = this.value.split(',');
      let fmt = this.config || 'Y-M-D H:I:S';
      return  s94Mjs.date(fmt, arr[0]*1000)+'至'+s94Mjs.date(fmt, arr[1]*1000);
    }
  },
  mounted() {
  }
}
</script>

<style scoped>

</style>